<?php

namespace frontend\models;

use Yii;
use yii\base\Model;

/**
 * ContactForm is the model behind the contact form.
 */
class DeviceForm extends Model {

    public $token;
    public $device = null;

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['token'], 'required'],
            ['token', 'string', 'max' => 32],
        ];
    }

    /**
     * Sends an email to the specified email address using the information collected by this model.
     *
     * @param string $email the target email address
     * @return bool whether the email was sent
     */
    public function register() {
        if (!$this->validate()) {
            throw new ModelHttpException('Token验证失败', $this->getFirstErrors());
        }
        if ($this->device == null) {
            throw new ModelHttpException('设备未注册或者验证访问超时');
        }
        $this->device->push_token = $this->token;
        return $this->device->save();
    }

}
